package com.csproj.dispatch.service;

import com.csproj.dispatch.domain.ProductDetailInfo;
import com.csproj.dispatch.domain.ProductInfo;
import com.csproj.dispatch.repository.ProductDetailInfoRepository;
import com.csproj.dispatch.repository.ProductInfoRepository;
import com.csproj.dispatch.service.dto.ProductDetailInfoDTO;
import com.csproj.dispatch.service.mapper.ProductDetailInfoMapper;
import com.csproj.siiat.base.common.dto.BaseDTO;
import com.csproj.siiat.security.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing ProductDetailInfo.
 */
@Service
@Transactional
public class ProductDetailInfoService {

    private final Logger log = LoggerFactory.getLogger(ProductDetailInfoService.class);

    private final ProductDetailInfoRepository productDetailInfoRepository;

    private final ProductInfoRepository productInfoRepository;

    private final ProductDetailInfoMapper productDetailInfoMapper;

    public ProductDetailInfoService(ProductDetailInfoRepository productDetailInfoRepository,
                                    ProductDetailInfoMapper productDetailInfoMapper, ProductInfoRepository productInfoRepository) {
        this.productDetailInfoRepository = productDetailInfoRepository;
        this.productDetailInfoMapper = productDetailInfoMapper;
        this.productInfoRepository = productInfoRepository;
    }

    /**
     * Save a productDetailInfo.
     *
     * @param productDetailInfoDTO the entity to save
     * @return the persisted entity
     */
    public ProductDetailInfoDTO save(ProductDetailInfoDTO productDetailInfoDTO) {
        log.debug("Request to save ProductDetailInfo : {}", productDetailInfoDTO);
        ProductDetailInfo productDetailInfo = productDetailInfoMapper.toEntity(productDetailInfoDTO);
        productDetailInfo = productDetailInfoRepository.save(productDetailInfo);
        return productDetailInfoMapper.toDto(productDetailInfo);
    }

    /**
     * Get all the productDetailInfos.
     *
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<ProductDetailInfoDTO> findAll() {
        log.debug("Request to get all ProductDetailInfos");
        return productDetailInfoRepository.findAll().stream()
            .map(productDetailInfoMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * 批量插入商品明细信息
     * @param productDetailInfoDTOS 商品明细信息列表
     * @return
     */
    public BaseDTO saveDetails(List<ProductDetailInfoDTO> productDetailInfoDTOS) {
        //必要信息检查
        if(productDetailInfoDTOS==null || productDetailInfoDTOS.isEmpty()){
            return new BaseDTO(BaseDTO.CODE_PARAM, "must_insert_product_info", null);
        }
        for(ProductDetailInfoDTO detailDto : productDetailInfoDTOS){
            detailDto.setId(null);
            if(detailDto.getInfoId()==null || "".equals(detailDto.getInfoId())){
                return new BaseDTO(BaseDTO.CODE_PARAM, "must_have_detail_info_key", null);
            }
            //查询货物信息是否存在
            ProductInfo productInfo = productInfoRepository.findOne(detailDto.getInfoId());
            if(productInfo==null || productInfo.getDeletionTime()!= null){
                return new BaseDTO(BaseDTO.CODE_PARAM, "product_info_no_exist", null);
            }
            detailDto.setCreationTime(ZonedDateTime.now());
            detailDto.setCreatorUserId(SecurityUtils.getCurrentUserId());
            detailDto.setLastModificationTime(ZonedDateTime.now());
            detailDto.setLastModifierUserId(SecurityUtils.getCurrentUserId());
        }


        //插入商品明细信息
        List productDetailInfos = productDetailInfoMapper.toEntity(productDetailInfoDTOS);
        productDetailInfos = productDetailInfoRepository.save(productDetailInfos);
        return BaseDTO.createBaseDTO(productDetailInfoMapper.toDto(productDetailInfos));
    }

    /**
     * 更新商品明细信息
     * @param id 商品明细主键
     * @param productDetailInfoDTO 商品明细信息
     * @return
     */
    public BaseDTO updateDetailById(String id, ProductDetailInfoDTO productDetailInfoDTO) {
        if(id==null || "".equals(id)){
            return new BaseDTO(BaseDTO.CODE_PARAM, "must_have_detail_key", null);
        }
        if(StringUtils.isEmpty(productDetailInfoDTO.getInfoId())){
            return new BaseDTO(BaseDTO.CODE_PARAM, "must_insert_product_info", null);
        }
        ProductDetailInfo productDetailInfo = productDetailInfoMapper.toEntity(productDetailInfoDTO);
        productDetailInfo.setId(id);
        productDetailInfo.setLastModifierUserId(SecurityUtils.getCurrentUserId());
        productDetailInfo.setLastModificationTime(ZonedDateTime.now());
        productDetailInfo = productDetailInfoRepository.save(productDetailInfo);
        return BaseDTO.createBaseDTO(productDetailInfoMapper.toDto(productDetailInfo));
    }

    /**
     * 删除商品明细信息
     * @param id 商品明细主键
     * @return
     */
    public BaseDTO deleteDetailById(String id) {
        if(id==null || "".equals(id)){
            return new BaseDTO(BaseDTO.CODE_PARAM, "product_info_key_not_null", null);
        }
        System.out.println(id);
        ProductDetailInfo productDetailInfo = productDetailInfoRepository.findOne(id);
        productDetailInfo.setDeletedFlag(true);
        productDetailInfo.setDeleterUserId(SecurityUtils.getCurrentUserId());
        productDetailInfo.setDeletionTime(ZonedDateTime.now());
        productDetailInfoRepository.save(productDetailInfo);
        return BaseDTO.createBaseDTO();
    }


}
